package amushen.base;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpSession;

public class BaseBO {	

	protected Double getD(String str){
		if(str==null||str.length()<1)return null;
		try{
			return new Double(str);
		}catch(Exception e){
			return null;
		}			
	}
	protected Integer getI(String str){
		if(str==null||str.length()<1)return null;
		try{
			return new Integer(str.trim());
		}catch(Exception e){
			return null;
		}			
	}
	protected Date getDate(String dd){
		if(dd==null||dd.length()<1)return null;		
		Date date;
		SimpleDateFormat sfd=new SimpleDateFormat("yyyy-MM-dd");
		try{
		date=sfd.parse(dd);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return date;		
	}
	
	/**
	 * decodeURI
	 * @param s
	 * @return
	 */
	protected String getS(String s){
		if(s==null)return null;
		String ret;
		try {
			ret = java.net.URLDecoder.decode(s, "UTF-8");
		} catch (UnsupportedEncodingException e) {			
			e.printStackTrace();
			return "";
		}
		return ret;
	}
	
	protected String getS(Object o){
		return getS((String)o);
	}
	
	
	/**
	 * get query condition
	 * because condition is null then return DBUtil.NOCONDITION
	 * @param obj
	 * @return
	 */
	protected String getCondition(Object obj){		
		String str=(String)obj;
		if(isNull(str))return DBUtil.NOCONDITION;
		str=getS(str);
		if(isNull(str))return DBUtil.NOCONDITION;
		return str;
	}
	
	protected boolean isNull(String str){
		if(str==null||str.length()<1)return true;
		return false;
	}
	
	protected boolean isNull(Object obj){
		if(obj==null)return true;
		return false;
	}
	
	protected HttpSession getSession(){
		return (HttpSession)BaseAction.httpSession.get();
	}
	
	
	/**
	 * default bo doing
	 * @param paras
	 * @param user
	 * @param is
	 * @return
	 */
	public Object defaultMethod(Map paras,String user,InputStream is,DBUtil db){
		return null;
	}
	
	/*
	protected Object setValue(Map map,Class clazz) throws Exception{
		Object obj=clazz.newInstance();
		PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(obj);
		int i;
		String pname=null;
		Class ptype=null;
		for(i=0;i<pds.length;i++){
			pname=pds[i].getName();
			ptype=pds[i].getPropertyType();
			
			if ((Object)ptype instanceof java.lang.Integer){
				BeanUtils.setProperty(obj, pname, getI((String)map.get(pname)));
			}else if((Object)ptype instanceof Double){
				BeanUtils.setProperty(obj, pname, getD((String)map.get(pname)));				
			}else if(ptype.isAssignableFrom(java.util.Date.class)){				
				BeanUtils.setProperty(obj, pname, getDate((String)map.get(pname)));
			}else{
				BeanUtils.setProperty(obj, pname, (String)map.get(pname));
			}
		}
		return obj;
	}
	*/
}
